package study.thread;

public class thread {
    public static int count = 0;
    public static void main(String[] args){
        //开启两个线程
        for(int i=0;i<2;i++){
            new Thread(
                    () -> {
                         try{
                             Thread.sleep(10);
                         } catch (InterruptedException e) {
                             e.printStackTrace();
                         }
                         //每个线程自增100次
                         for (int j=0;j<100;j++){
                                 count++;
                         }
                     }
            ).start();
        }
        try{
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("count="+count);
    }
    /*public final boolean compareAndSet(int expect , int update){
        return unsafe.compareAndSwapInt(this,valueOffset,expect,update);
    }
    //private static final Unsafe unsafe = Unsafe.getUnsafe();
    try{
        Field field = null;
        try {
            field = Unsafe.class.getDeclaredField("theUnsafe");
        } catch (NoSuchFieldException ex) {
            ex.printStackTrace();
        }
        assert field != null;
        field.setAccessible(true);
        try {
            Unsafe unsafe = (Unsafe) field.get(null);
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        }
    }
    private static final long valueOffset ;

    static {
        try{
            valueOffset = unsafe.objectFieldOffset
                    (AtomicInteger.class.getDeclaredField("value"));
        } catch (Exception e) {
            throw new Error(e);
        }
    }*/
}
